import 'package:find_friend/common/alert_tool.dart';
import 'package:find_friend/const/const.dart';
import 'package:find_friend/network/network.dart';
import 'package:find_friend/pages/login&register/login.dart';
import 'package:find_friend/pages/login&register/login_textfield.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:page_transition/page_transition.dart';

class MineSettingModPwdPage extends StatefulWidget {
  @override
  _MineSettingModPwdPageState createState() => _MineSettingModPwdPageState();
}

class _MineSettingModPwdPageState extends State<MineSettingModPwdPage> {

  final TextEditingController _repasswordController = TextEditingController();
  final TextEditingController _newpasswordController = TextEditingController();
  final TextEditingController _renewpasswordController = TextEditingController();
  final _repasswordFoucs = FocusNode();
  final _newpasswordFoucs = FocusNode();
  final _renewpasswordFoucs = FocusNode();

  @override 
  dispose(){
    _repasswordController.dispose();
    _newpasswordController.dispose();
    _renewpasswordController.dispose();
    super.dispose();
  }

  _requestMod() {
    if(_repasswordController.text == ""){
      AlertTool.showToast("请输入密码");
      return;
    }

    if(_newpasswordController.text == ""){
      AlertTool.showToast("请输入新密码");
      return;
    }

    if(_renewpasswordController.text == ""){
      AlertTool.showToast("请输入确认新密码");
      return;
    }

    if(!isLoginPassword(_newpasswordController.text)) {
      AlertTool.showToast("请输入至少6个数字、字母组合的密码");
      return;
    }

    if(_newpasswordController.text != _renewpasswordController.text) {
      AlertTool.showToast("两次输入密码不一致");
      return;
    }

    Networktool.request(API.resetpwd, params: {"new_pwd":_renewpasswordController.text, "old_pwd":_repasswordController.text}, success: (data){
      AlertTool.showMsgDialog(context, msg:"您已修改密码成功\n请重新登录", callback: (){
          Navigator.of(context).pushAndRemoveUntil(
            PageTransition(
              type: PageTransitionType.fade,
              child: LoginScreen()
            )
          ,(route) => false);
        });
    },fail: (data){
      AlertTool.showToast(data);
    });

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomPadding: false,
      appBar: AppBar(
        title: Text("修改密码"),
        backgroundColor: Colors.white,
        centerTitle: true,
        leading: IconButton(
          onPressed: (){
            Navigator.of(context).pop();
          },
          icon: Image.asset("assets/fanhui.png"),
        ),
        actions: [
          FlatButton(
            child: Text("完成", style: instanceTextStyle(15, Colors.black, fontWeight: FontWeight.normal)),
            onPressed: _requestMod,
          ),
        ],
      ),
      body: SafeArea(
        child: GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: (){

          },
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 37.5),
            child: Column(
              children: [
                Padding(
                  padding: const EdgeInsets.symmetric(vertical: 25),
                  child: LoginTextField(
                    focusNode: _repasswordFoucs,
                    controller: _repasswordController,
                    placeholder: "请输入原始密码",
                    obscureText: true,
                  ),
                ),
                PwdModTextfield(
                  focusNode: _newpasswordFoucs,
                  controller: _newpasswordController,
                  placeholder: "请输入新密码",
                  obscureText: true,
                ),
                SizedBox(height: 25,),
                PwdModTextfield(
                  focusNode: _renewpasswordFoucs,
                  controller: _renewpasswordController,
                  placeholder: "确认新密码",
                  obscureText: true,
                ),
                Padding(
                  padding: const EdgeInsets.only(top:10.0),
                  child: Text("请输入至少6个数字、字母组成的密码", style: instanceTextStyle(11, APP_font_grey),),
                ),
                Spacer(),
                GestureDetector(
                  onTap: _requestMod,
                  child: Container(
                    height: 40,
                    margin: const EdgeInsets.symmetric(horizontal: 25),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(20),
                      border: Border.all(color: Colors.black, width: 1.5)
                    ),
                    child: Text("确认", style: instanceTextStyle(18, Colors.black),),
                  ),
                ),
                SizedBox(height: 50,)
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class PwdModTextfield extends StatefulWidget {
  final TextEditingController controller;
  final String placeholder;
  final bool obscureText;
  final int maxlength;
  final double marginTop;
  bool isshowclear;
  final List<TextInputFormatter> inputFormatters;
  final FocusNode focusNode;
  final TextInputType keybordType;
  final Function(String str) callback;

 PwdModTextfield({Key key, 
    this.controller, 
    this.placeholder, 
    this.obscureText = false, 
    this.maxlength, 
    this.marginTop, 
    this.isshowclear = false, 
    this.inputFormatters, 
    this.focusNode, 
    this.keybordType, 
    this.callback,
  }) : super(key: key);

  @override
  _PwdModTextfieldState createState() => _PwdModTextfieldState();
}

class _PwdModTextfieldState extends State<PwdModTextfield> {

  bool isshowsuccess = false;
  @override
  void initState() {
    super.initState();
    if(widget.focusNode != null){
      widget.focusNode.addListener(() { 
        if(this.mounted){
          setState(() {
            widget.isshowclear = widget.focusNode.hasFocus;
          });
        }
      });
    }
  }

   @override
  void dispose() {
    if(widget.focusNode != null)widget.focusNode.removeListener(() { });
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Container(
              height: 40,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(20),
                border: Border.all(color: Colors.transparent, width: 1.5)
              ),
              child: TextField(
                maxLength: widget.maxlength,
                controller: widget.controller,
                obscureText: widget.obscureText,
                autocorrect: false,
                focusNode: widget.focusNode,
                keyboardType: widget.keybordType,
                inputFormatters: widget.inputFormatters,
                decoration: InputDecoration(
                  hintStyle: instanceTextStyle(11, const Color(0xff999999)),
                  hintText: widget.placeholder,
                  focusedBorder: OutlineInputBorder(
                    borderRadius: const BorderRadius.all(Radius.circular(20)),
                    borderSide: BorderSide(width: 1.5, color: Colors.black),
                  ),
                  enabledBorder: OutlineInputBorder(
                    borderRadius: const BorderRadius.all(Radius.circular(20)),
                    borderSide: BorderSide(width: 1.5, color: Colors.black),
                  ),
                  border: OutlineInputBorder(
                    borderRadius: const BorderRadius.all(Radius.circular(20)),
                    borderSide: BorderSide(width: 1.5, color: Colors.black),
                  ),
                  suffixIcon: widget.isshowclear ? IconButton(
                    icon: Image.asset(isshowsuccess ? "assets/chenggong.png" : "assets/zhuce-x.png"),
                    onPressed: (){
                      if(!isshowsuccess)widget.controller.clear();
                    }
                  ): null,
                  counterText: "",
                  contentPadding: const EdgeInsets.only(left: 27)
                ),
                onTap: (){
                  setState(() {
                    widget.isshowclear = true;
                  });
                },
                onChanged: (str){ // 输入验证码的时候需要
                  setState(() {
                    isshowsuccess = isLoginPassword(str);
                  });
                  if(widget.callback != null) widget.callback(widget.controller.text);
                },
                onEditingComplete: (){
                  widget.focusNode.unfocus();
                  setState(() {
                    widget.isshowclear = false;
                  });
                  if(widget.callback != null) widget.callback(widget.controller.text);
                },
              ),
            );
  }
}